package hlhj.fhp.tvlib.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.activitys.NewLiveAty
import hlhj.fhp.tvlib.adapter.LiveNewAdp
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.LiveListBean
import kotlinx.android.synthetic.main.activity_home_aty.view.*
import kotlinx.android.synthetic.main.activity_live_list_aty.*
import java.util.ArrayList

/**
 * Created by Never Fear   on 2018\9\6 0006.
Never More....
 */
class HomeLiveFgm :Fragment(), SpringView.OnFreshListener {
    private lateinit var rootView:View
    private var page=1
    private lateinit var loadingDialog: LoadingDialog
    private var adp :LiveNewAdp?=null
    private val datas= ArrayList<LiveListBean.DataBean>()

    companion object {
        var tvTitle="直播"
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.home_live_fgm,null)
        loadingDialog=LoadingDialog(context!!)
        rootView.spView.header=DefaultHeader(context)
        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.setListener(this)
        setRcy()
        return rootView
    }

    private fun setRcy(){
        adp= LiveNewAdp(datas)
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context!!,LinearLayoutManager.VERTICAL,false)


        adp?.setOnItemClickListener { adapter, view, position ->
            datas[position].read_num++
            rootView.recyclerview.postDelayed({
                adapter.notifyDataSetChanged()
            },1000)
            val intent = Intent(context!!, NewLiveAty::class.java)
            intent.putExtra("id",datas[position].id)
            intent.putExtra("imgThumb",datas[position].live_thumb)
            context?.startActivity(intent)

        }

        getData()
    }
    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+ Contents.LIVE_LIST)
                .params("page",page)
                .headers("token",TMSharedPUtil.getTMToken(context))
                .params("token",TMSharedPUtil.getTMToken(context))
                .execute(object : StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        spView.onFinishFreshAndLoad()
                        val str = response?.body()
                        val listBean = Gson().fromJson(str, LiveListBean::class.java)
                        if (listBean.code==1){
                            datas.addAll(listBean.data)
                            adp!!.notifyDataSetChanged()
                        }else{
                            Toast.makeText(context,listBean.msg, Toast.LENGTH_SHORT).show()
                        }
                    }
                })
    }

    override fun onLoadmore() {
        page++
        getData()
    }

    override fun onRefresh() {
        page=1
        datas.clear()
        getData()
    }
}